package com.haidechizi.i18n.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * 全局语言设置文件
 * 通过ThreadLocal设置本次的语言
 */
@Slf4j
public class GlobalUtils {

    /**
     * 设置当前系统是否跳过国际化翻译
     */
    private static final ThreadLocal<Boolean> skipSystemLimit = new ThreadLocal<>();
    private static ThreadLocal<String> languageCache = new ThreadLocal<>();
    private static ThreadLocal<String> translateCache = new ThreadLocal<>();

    public static String getLanguage() {
        return languageCache.get();
    }

    public static void setLanguage(String value) {
        languageCache.set(value);
    }

    public static void removeLanguage() {
        languageCache.remove();
    }

    public static String getTranslate() {
        return translateCache.get();
    }

    public static void setTranslate(String translate) {
        translateCache.set(translate);
    }

    public static void removeTranslate() {
        translateCache.remove();
    }

    /**
     * 默认是false
     *
     * @return
     */
    public static Boolean skipSystemLimit() {
        return Optional.ofNullable(skipSystemLimit.get()).orElse(false);
    }

    public static void setSkipSystemLimit(Boolean skipLimit) {
        skipSystemLimit.set(skipLimit);
    }

    public static void removeSkipSystemLimit() {
        skipSystemLimit.remove();
    }

    public static void clear() {
        removeLanguage();
        removeTranslate();
        removeSkipSystemLimit();
    }

}
